﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

using Fuse8.DomainFramework.BusinessService;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Web.MvcPowered;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsPageControlController : CmsAdminController<CmsPageControl>
    {
        private readonly CmsPageControlService _pageControlService = new CmsPageControlService();
        private readonly CmsModuleService _moduleService = new CmsModuleService();

        [ChildActionOnly]
        public ActionResult AvailableModules(int pageId, int templateRegionId)
        {
            ViewBag.PageId = pageId;
            ViewBag.CurrentTemplateRegion = templateRegionId;

            var data = _moduleService.FindAll();

            return PartialView(data ?? new List<CmsModule>());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Save(FormCollection elements)
        {
            ActionResult result = null;

            if (elements != null)
            {
                int pageControlId = 0;
                if (!string.IsNullOrWhiteSpace(elements["PageControlId"]) && 
                    int.TryParse(elements["PageControlId"], out pageControlId))
                {
                    var pageControl = _pageControlService.FindOne(pageControlId);
                    if (pageControl != null)
                    {
                        if (!string.IsNullOrWhiteSpace(elements["PageControlContentId"]))
                        {
                            //page content
                            pageControl.PageControlContentId = int.Parse(elements["PageControlContentId"]);
                            pageControl.PageControlContent = HttpContext.Server.HtmlDecode(elements["PageControlContent"]);
                        }
                        else if (!string.IsNullOrWhiteSpace(elements["ModuleId"]))
                        {
                            //page module
                            pageControl.ModuleId = int.Parse(elements["ModuleId"]);
                            pageControl.SaveCmsModuleParameters(HttpContext, elements);
                        }
                        else
                        {
                            //another type of page control
                            pageControl.SaveCmsMvcPartialControlParameters(HttpContext, elements);
                        }

                        using (var transaction = new BusinessServiceTransaction<CmsPageControl>())
                        {
                            _pageControlService.Save(pageControl);
                        }

                        result = Json(1);
                    }
                }
            }

            return result ?? new EmptyResult();
        }

        [HttpPost]
        public ActionResult MoveUp(int pageControlId)
        {
            ActionResult result = null;

            if (pageControlId > 0)
            {
                _pageControlService.Move(pageControlId, true);
                result = Json(1);
            }

            return result ?? new EmptyResult();
        }

        [HttpPost]
        public ActionResult MoveDown(int pageControlId)
        {
            ActionResult result = null;

            if (pageControlId > 0)
            {
                _pageControlService.Move(pageControlId, false);
                result = Json(1);
            }

            return result ?? new EmptyResult();
        }
        
        protected override bool _Delete(int id)
        {
            bool result = false;

            if (id > 0)
            {
                _pageControlService.Remove(id);

                result = true;
            }

            return result;
        }
    }
}
